﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using KMS.Core.domain.model.project;
using KMS.Core.application;
using KMS.Web.Controllers;
using Moq;
using System.Web.Mvc;
using KMS.Core.domain.model.alert;
using KMS.Web.Views.Projects;
using KMS.Core.domain.model.post;
using KMS.Web.Tests.Factory;
using KMS.Web.Views.Shared;

namespace KMS.Web.Tests.Controllers
{
    /// <summary>
    /// Summary description for ProjectControllerTests
    /// </summary>
    [TestClass]
    public class ProjectControllerTests
    {
        public ProjectControllerTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void FindProjectsForCurrentUserActionReturnsProjectViewModelsFromService()
        {
            var projects = new List<Project> { new Project("project 1"), new Project("project 2"), new Project("project 3") };
            var projectServiceMock = new Mock<IProjectsService>();
            projectServiceMock.Setup(s => s.FindProjectsForCurrentUser()).Returns(projects);

            var controller = new ProjectsController(projectServiceMock.Object);

            var result = (ViewResult)controller.UserProjects();
            var returnedProjects = (IList<ProjectViewModel>)result.ViewData.Model;

            Assert.AreEqual(3, returnedProjects.Count, "Result list should contain 3 objects");
            Assert.AreEqual("project 1", returnedProjects[0].Name, "wrong projects[0] name");
            Assert.AreEqual("project 2", returnedProjects[1].Name, "wrong projects[1] name");
            Assert.AreEqual("project 3", returnedProjects[2].Name, "wrong projects[2] name");
        }

        [TestMethod]
        public void ProjectControllerGoToActionReturnsProjectViewModelFromService()
        {
            var postFactory = new PostFactory();
            var resources = new List<Resource> { postFactory.CreateResourceWithTitle("my resource"), 
                postFactory.CreateResourceWithTitle("cool resource"), 
                postFactory.CreateResourceWithTitle("tutorial") };

            var project = new Project("test project");

            var projectServiceMock = new Mock<IProjectsService>();
            projectServiceMock.Setup(s => s.FindProject(It.Is<int>(id => id == 5))).Returns(project);
            projectServiceMock.Setup(s => s.FindResourcesForProject(It.Is<int>(id => id == 5))).Returns(resources);

            var controller = new ProjectsController(projectServiceMock.Object);
            var result = (ViewResult)controller.GoTo(5);
            var returnedProjectViewModel = (ProjectViewModel)result.ViewData.Model;

            Assert.AreEqual("test project", returnedProjectViewModel.Name);
            Assert.AreEqual(3, returnedProjectViewModel.Resources.Count, "resources list shold contain 3 resources");
            Assert.AreEqual("my resource", returnedProjectViewModel.Resources[0].Title, "wrong resources[0] title");
            Assert.AreEqual("cool resource", returnedProjectViewModel.Resources[1].Title, "wrong resources[1] title");
            Assert.AreEqual("tutorial", returnedProjectViewModel.Resources[2].Title, "wrong resources[2] title");
        }

        [TestMethod]
        public void ProjectControllerAddResourceActionReturnsViewModelWithProjectName()
        {
            var project = new Project("my project");

            var projectServiceMock = new Mock<IProjectsService>();
            projectServiceMock.Setup(s => s.FindProject(It.Is<int>(id => id == 10))).Returns(project);

            var controller = new ProjectsController(projectServiceMock.Object);
            var result = (ViewResult)controller.AddResource(10);
            var returnedResourceViewModel = (ResourceViewModel)result.ViewData.Model;
            Assert.AreEqual("my project", returnedResourceViewModel.Project.Name, "project name");
            Assert.IsTrue(string.IsNullOrEmpty(returnedResourceViewModel.Title), "title");
            Assert.IsTrue(string.IsNullOrEmpty(returnedResourceViewModel.Content), "content");
        }

        [TestMethod]
        public void ProjectControllerAddResourcePostActionSavesResourceViaService()
        {
            var resourceViewModel = new ResourceViewModel
            {
                Title = "resource title",
                Content = "some content"
            };

            var projectServiceMock = new Mock<IProjectsService>();            

            var controller = new ProjectsController(projectServiceMock.Object);

            controller.AddResource(resourceViewModel, 10);

            projectServiceMock.Verify(s => s.AddResourceToProject(
                It.Is<string>(title => title == "resource title"),
                It.Is<string>(content => content == "some content"),
                It.Is<int>(id => id == 10)));
            //projectServiceMock.Setup(s => s.FindProject(It.Is<int>(id => id == 10))).Returns(project);
        }
    }
}
